package com.gemantic.client.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gemantic.client.service.UserAccessService;
import com.gemantic.config.DIYRestTemplate;

@Controller
@CrossOrigin
public class SsoClientController {
	
	Logger logger = LoggerFactory.getLogger(SsoClientController.class);
		
	@Resource
    private DIYRestTemplate restTemplate;
	
	@Value("${sso.server.url}")
	String ssoServerPath;
	
	@Autowired
	private UserAccessService userAccessService;
	
	@RequestMapping("/receiveToken")
	@ResponseBody
	public String receiveToken(HttpServletRequest request, String ssoToken,String userName) {
		if(ssoToken!=null && ssoToken.toString().trim().length()>0) {
			String realUrl = request.getRequestURL().toString();
			String[] paths = realUrl.split("/");
			String realUrlUrls = paths[2];
			String returnUrl = ssoServerPath+"/verifyToken?address="+realUrlUrls+"&token="+ssoToken;
			//String returnUrl = "http://damon-auth/varifyToken?address="+realUrlUrls+"&token="+ssoToken;
			//http://peer2:8089/verifyToken?address=peer1:8088&token=c2ce29be-5adb-4aaf-82cc-2ba24330176e
			/*Map<String, Object> param = new HashMap<>();
			param.put("realUrlUrls", realUrlUrls);
			param.put("ssoToken", ssoToken);
			ResponseEntity<Object> resu = restTemplate.exchange(returnUrl+"?address={realUrlUrls}&token={ssoToken}", HttpMethod.GET, null, new ParameterizedTypeReference<String>() {}, param);*/
			logger.info("verifyToken ....");
			Object resu = restTemplate.getForObject(returnUrl, Object.class);
			logger.info(" verifyToken  resp status:" + resu);
			if("SUCCESS".equals(resu)) {
				logger.info("==========================login success================================");
				//创建局部会话，保存用户状态为已登陆
				userAccessService.putUserStatus(userName, ssoToken);
				return "success";
			}
		}
		return "error";
	}
	
	@RequestMapping("/ssoLogout")
	@ResponseBody
	public String ssoLogout(String userName) {
		String userToken = userAccessService.getUserToken(userName);
		if(userToken!=null) {
			String returnUrl = ssoServerPath+"/logoutByToken?ssoToken="+userToken;
			return restTemplate.getForObject(returnUrl, String.class);
		}
		return "None Token";
	}
	
	@RequestMapping("/ssoDeleteToken")
	@ResponseBody
	public String ssoDeleteToken(String ssoToken) {
		userAccessService.deleteToken(ssoToken);
		return "success";
	}
}
